package com.im.partchat.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.im.partchat.App;
import com.im.partchat.R;
import com.im.partchat.base.BaseActivity;
import com.im.partchat.bean.User;
import com.im.partchat.db.dao.UserDao;
import com.im.partchat.mvp.persenter.FriendPresenter;
import com.im.partchat.widget.GlideCircleTransform;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import io.rong.imkit.RongIM;

/**
 * Created by hhz on 2017/7/1.
 */
public class PersonActivity extends BaseActivity {

    @BindView(R.id.head_imgview)
    ImageView headImgview;
    @BindView(R.id.name_textview)
    TextView nameTextview;
    @BindView(R.id.dept_name)
    TextView deptName;
    @BindView(R.id.textview_tel_no)
    TextView textviewTelNo;
    @BindView(R.id.call_imageview)
    ImageView callImageview;
    @BindView(R.id.sendmsg_imageview)
    ImageView sendmsgImageview;
    @BindView(R.id.send_msg_btn)
    Button sendMsgBtn;
    @BindView(R.id.add_friend_btn)
    Button addFriendBtn;

    private User user;

    private FriendPresenter presenter;

    public static void startPersonActivity(Context context, String userId) {
        Intent mIntent = new Intent(context, PersonActivity.class);
        mIntent.putExtra("user_id", userId);
        context.startActivity(mIntent);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_person;
    }

    @Override
    protected void initData() {
        presenter = new FriendPresenter(this, this);
        final String userId = getIntent().getStringExtra("user_id");
        Observable.create(new ObservableOnSubscribe<User>() {
            @Override
            public void subscribe(ObservableEmitter<User> e) throws Exception {
                User user = UserDao.getInstance().getUserById(userId);
                e.onNext(user);
            }
        }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<User>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(User o) {
                        if (o == null) {
                            showToast("用户信息有误");
                            finish();
                        } else {
                            user =  o;
                            Glide.with(App.getApplication())
                                    .load(TextUtils.isEmpty(user.getHeadUrl()) ? "" : user.getHeadUrl())
                                    .transform(new GlideCircleTransform(PersonActivity.this))
                                    .placeholder(R.drawable.rc_ic_def_msg_portrait)
                                    .error(R.drawable.rc_ic_def_msg_portrait)
                                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                                    .into(headImgview);
                            nameTextview.setText(user.getName());
                            deptName.setText(user.getPosition());
                            textviewTelNo.setText(user.getPhone());
                            if (user.isFriend()) {
                                sendMsgBtn.setVisibility(View.VISIBLE);
                                addFriendBtn.setVisibility(View.GONE);
                            } else {
                                sendMsgBtn.setVisibility(View.GONE);
                                addFriendBtn.setVisibility(View.VISIBLE);
                            }
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });

    }

    @Override
    public void onLoadDataSuccess(Object o, String flag) {

    }

    @OnClick({R.id.call_imageview, R.id.sendmsg_imageview, R.id.send_msg_btn, R.id.add_friend_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.call_imageview:
                if (user != null && !TextUtils.isEmpty(user.getPhone())) {
                    call(user.getPhone());
                }
                break;
            case R.id.sendmsg_imageview:
                sendEms();
                break;
            case R.id.send_msg_btn:
                RongIM.getInstance().startPrivateChat(this, user.getId(), user.getName());
                break;
            case R.id.add_friend_btn:
                presenter.friendApplyAdd(App.getUserBean().getId(), user.getId());
                break;
        }
    }

    /**
     * 拨打电话
     */
    private void call(String mobile) {
        Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + mobile));
        startActivity(intent);
    }

    /**
     * 发送短信
     */
    private void sendEms() {
        if (user != null && !TextUtils.isEmpty(user.getPhone())) {
            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_SENDTO);
            intent.setData(Uri.parse("smsto:" + user.getPhone()));
            startActivity(intent);
        }
    }

}
